package thread_ex;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;

public class BallBreakBound extends Canvas implements KeyListener, Runnable, MouseListener{
	
	JFrame frame;
	int size = 20;
	int[][] map = {	{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,3,3,3,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,3,3,3,3,3,0,0,0,0,0,1},
					{1,0,0,0,0,3,3,3,3,3,3,3,0,0,0,0,1},
					{1,0,0,0,3,3,3,3,3,3,3,3,3,0,0,0,1},
					{1,0,0,0,0,0,0,3,3,3,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
					{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
			};
	int x = 6;
	int y = 16;
	int boardX = 6;
	int boardY = 17;
	boolean flagX = true;
	boolean flagY = true;
	Thread t = new Thread();
	
	public BallBreakBound(){
		frame = new JFrame("벽돌깨기 게임");
		frame.add(this);
		frame.setDefaultCloseOperation(3);
		frame.addKeyListener(this);
		frame.setSize(320+8+20, 380+27+20);
		frame.setVisible(true);
	}
	
	public void run(){
		while(true){

			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			map[boardY][boardX] = 2;
			map[boardY][boardX+1] = 2;
			map[boardY][boardX+2] = 2;
			map[boardY][boardX+3] = 2;
			
			repaint();
		}
	}
	
	public void paint(Graphics g){			
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, getWidth(), getHeight());
			//System.out.println("x="+x+", y="+y+", map="+map[y][x]);			
			
			for(int i = 0; i<17; i++){
				for(int j = 0; j<20; j++){
					if(map[j][i] == 1) {
						g.setColor(Color.BLACK);
						g.fillRect(i*size, j*size, size, size);
					}
					else if(map[j][i] == 2){
						g.setColor(Color.BLUE);
						g.fillRect(i*size, j*size, size, size);
						g.setColor(Color.BLACK);
						g.drawRect(i*size, j*size, size, size);
					}
					else if(map[j][i] == 3){
						g.setColor(Color.YELLOW);
						g.fillRect(i*size, j*size, size, size);
						g.setColor(Color.BLACK);
						g.drawRect(i*size, j*size, size, size);
					}					
				}
			}
			
			g.setColor(Color.RED);
			g.fillRoundRect(x*size, y*size, size, size, size, size);
			//g.fillRect(x*size, y*size, size, size);
			g.setColor(Color.BLACK);
			g.drawRoundRect(x*size, y*size, size, size, size, size);
			//g.drawRect(x*size, y*size, size, size);
					
			if(flagX == false) x++;
			else x--;
			
			if(flagY == false) y++;
			else y--;
						
			if(x>14) flagX = true;
			if(y>17) flagY = true;
			if(x<2) flagX = false;
			if(y<2) flagY = false;

			if(flagX == true && flagY == false){
				if(map[y+1][x-1] == 2)flagY = true;
			}
			else if(flagX == false && flagY == false){
				if(map[y+1][x+1] == 2)flagY = true;
			}
			
			if(flagX == true && flagY == true){
				if(map[y-1][x-1] == 3){
					map[y-1][x-1] = 0;
					flagY = !flagY;
				}
			}
			else if(flagX == true && flagY == false){
				if(map[y+1][x-1] == 3){
					map[y+1][x-1] = 0;
					flagX = !flagX;
				}
			}
			else if(flagX == false && flagY == true){
				if(map[y-1][x+1] == 3){
					map[y-1][x+1] = 0;
					flagX = !flagX;
				}
			}
			else if(flagX == false && flagY == false){
				if(map[y+1][x+1] == 3){
					map[y+1][x+1] = 0;
					flagY = !flagY;
				}
			}
			
	}
	
	public void update(Graphics g){
		paint(g);
	}
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == e.VK_RIGHT){
			if(boardX<12) {
				this.boardX++;
				map[boardY][boardX-1] = 0;
			}
		}
		else if(e.getKeyCode() == e.VK_LEFT){
			if(boardX>1) {
				this.boardX--;
				map[boardY][boardX+4] = 0;
			}
		}		
	}
	public void keyReleased(KeyEvent e){
		
	}
	public void keyTyped(KeyEvent e){
		
	}
	
	public static void main(String[] args){
		BallBreakBound bb = new BallBreakBound();
		Thread t = new Thread(bb);
		t.start();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}	
}
